package com.lanou.dllo.baisibudejie.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TabHost;

import com.lanou.dllo.baisibudejie.fragment.CardFragment;
import com.lanou.dllo.baisibudejie.fragment.MyFragment;
import com.lanou.dllo.baisibudejie.fragment.NewsFragment;
import com.lanou.dllo.baisibudejie.fragment.PostFragment;
import com.lanou.dllo.baisibudejie.R;

/**
 * Created by dllo on 15/10/22.
 */
public class FirstPageActivity extends FragmentActivity {
    private TabHost tabHost;
    private FragmentManager fm;
    public boolean isLogin;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置布局文件
        setContentView(R.layout.activity_first_page);
        fm = getSupportFragmentManager();
        initTabHost();//初始化tabhost
        initView();//初始化页面
    }

    private void initTabHost() {
        //初始化TabHost
        tabHost = (TabHost) findViewById(android.R.id.tabhost);
        tabHost.setup();

        TabHost.TabSpec tabSpec_card = tabHost.newTabSpec(getString(R.string.card));
        View card_view = getLayoutInflater().inflate(R.layout.tab_card_item, null);
        tabSpec_card.setIndicator(card_view);
        tabSpec_card.setContent(R.id.tabcontent_card);
        tabHost.addTab(tabSpec_card);

        TabHost.TabSpec tabSpec_news = tabHost.newTabSpec(getString(R.string.news));
        View news_view = getLayoutInflater().inflate(R.layout.tab_news_item, null);
        tabSpec_news.setIndicator(news_view);
        tabSpec_news.setContent(R.id.tabcontent_news);
        tabHost.addTab(tabSpec_news);

        TabHost.TabSpec tabSpec_post = tabHost.newTabSpec(getString(R.string.post));
        View post_view = getLayoutInflater().inflate(R.layout.tab_post_item, null);
        tabSpec_post.setIndicator(post_view);
        tabSpec_post.setContent(R.id.tabcontent_post);
        tabHost.addTab(tabSpec_post);

        TabHost.TabSpec tabSpec_my = tabHost.newTabSpec(getString(R.string.my));
        View my_view = getLayoutInflater().inflate(R.layout.tab_my_item, null);
        tabSpec_my.setIndicator(my_view);
        tabSpec_my.setContent(R.id.tabcontent_my);
        tabHost.addTab(tabSpec_my);

    }

    private void initView() {
        //替换占位符
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.tabcontent_card, new CardFragment());
        transaction.replace(R.id.tabcontent_my, new MyFragment(true));
        transaction.replace(R.id.tabcontent_news, new NewsFragment());
        transaction.replace(R.id.tabcontent_post, new PostFragment());
        transaction.commit();
    }


}
